<?php

namespace catchAdmin\model;

class LiuYan extends ModelApp
{

    protected $append = [
        'audit_status_name',
        'audit_time_show',
        'zhu_ti_name',
    ];

    protected $type = [
        'img'    => 'array',
        'zhu_ti' => 'array',
    ];


    ############################ 关系 start ###############################

    public function wechatUser()
    {
        return $this->hasOne(WechatUser::class, 'id', 'wechat_user_id');
    }


    ############################ 关系 end ###############################
    ############################ Attr start ###############################

    /**
     * audit_time_show
     */
    public function getAuditTimeShowAttr()
    {
        if (empty($this->getOrigin('audit_time'))) {
            return "——";
        }

        return date('Y-m-d H:i:s', $this->getOrigin('audit_time'));
    }


    /**
     * audit_status_name
     */
    public function getAuditStatusNameAttr()
    {
        $list = [
            '1' => '审核中',
            '2' => '通过',
            '3' => '拒绝',
        ];

        return $list[$this->getOrigin('audit_status')] ?? "";
    }


    public function getZhuTiList()
    {
        return [
            1 => '二手物资',
            2 => '物品租赁',
            3 => '业务合作',
            4 => '相亲交友',
            5 => '人生感悟',
            6 => '公益慈善',
            7 => '杂七杂八',
        ];
    }


    /**
     * zhu_ti_name
     */
    public function getZhuTiNameAttr()
    {
        $arr = [];
        if (is_array($this->zhu_ti)) {
            foreach ($this->zhu_ti as $k => $v) {
                $arr[] = $this->getZhuTiList()[$v] ?? "";
            }
        }

        return implode('，', $arr);
    }


    ############################ Attr end ##########################
    ############################ Search start ##########################

    public function searchContentAttr($query, $value, $data)
    {
        $query->where('content', 'like', '%' . $value . '%');
    }


    public function searchTitleAttr($query, $value, $data)
    {
        $query->where('title', 'like', '%' . $value . '%');
    }


    ############################ Search end ##########################

    public function saveDataApp($param)
    {
        $msg_arr = [
            'id'      => $param['id'] ?? 0,
            'to_id'   => $param['to_id'] ?? 0,
            'title'   => $param['title'] ?? '',
            'zhu_ti'  => $param['zhu_ti'] ?? 0,
            'content' => $param['content'] ?? '',
            'img'     => $param['img'] ?? [],

            'wechat_user_id' => $this->getLoginId(),
            'audit_status'   => 1,
            'audit_note'     => '',
            'audit_time'     => 0,
        ];

        if (empty($msg_arr['to_id'])) {
            $msg_arr['num_no'] = date('YmdHis') . "_" . rand(1000, 9999);
        } else {
            $to_msg = $this
                ->where('id', $param['to_id'])
                ->find();

            $msg_arr['num_no'] = $to_msg['num_no'] ?? '';
        }

        return parent::saveDataCC($msg_arr);
    }
}
